package com.senyint.sys.utils.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Map;
import java.util.Properties;

import com.senyint.sys.utils.FileUtils;

/**
 * 配置文件读取工具类
 * 
 * @author NaiFei Wang
 * @version 1.0
 */
public class PropertiesUtil {

	/**
	 * props 保存为properties文件
	 * 
	 * @see 文件存在时为覆盖
	 * @param props
	 * @param propsFile
	 * @param comments
	 */
	public static boolean writeToPropsFile(Properties props, File propsFile, String comments, String charsetName) {
		boolean r = false;
		FileOutputStream outputStream = null;
		OutputStreamWriter osw = null;
		try {
			outputStream = new FileOutputStream(propsFile);
			osw = new OutputStreamWriter(outputStream, charsetName);
			props.store(osw, comments);
			r = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.flush();
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (osw != null) {
				try {
					osw.flush();
					osw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return r;
	}

	/**
	 * props 保存为properties文件
	 * 
	 * @param props
	 * @param propsFile
	 * @param comments
	 * @return
	 */
	public static boolean writeToPropsFile(Properties props, File propsFile, String comments) {
		return writeToPropsFile(props, propsFile, comments, "utf-8");
	}

	/**
	 * props 保存为properties文件
	 * 
	 * @param props
	 * @param propsFile
	 * @return
	 */
	public static boolean writeToPropsFile(Properties props, File propsFile) {
		return writeToPropsFile(props, propsFile, "");
	}

	/**
	 * props 保存为xml文件
	 * 
	 * @param props
	 * @param xmlFile
	 * @param comments
	 * @param encoding
	 * @return
	 */
	public static boolean writeToXMLFile(Properties props, File xmlFile, String comments, String encoding) {
		boolean r = false;
		FileOutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(xmlFile);
			props.storeToXML(outputStream, comments, encoding);
			r = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.flush();
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return r;
	}

	/**
	 * props 保存为xml文件
	 * 
	 * @param props
	 * @param xmlFile
	 * @param comments
	 * @return
	 */
	public static boolean writeToXMLFile(Properties props, File xmlFile, String comments) {
		return writeToXMLFile(props, xmlFile, comments, "utf-8");
	}

	/**
	 * props 保存为xml文件
	 * 
	 * @param props
	 * @param propsFile
	 * @return
	 */
	public static boolean writeToXMLFile(Properties props, File propsFile) {
		return writeToXMLFile(props, propsFile, "");
	}

	/**
	 * 读取xml文件
	 * 
	 * @see prop
	 * @param xmlInputStream
	 * @return
	 */
	public static Properties loadFromXML(InputStream xmlInputStream) {
		Properties props = null;
		if (xmlInputStream != null) {
			try {
				props = new Properties();
				props.loadFromXML(xmlInputStream);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					xmlInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return props;
	}

	/**
	 * 读取Properties文件
	 * 
	 * @param fileInputStream
	 * @param charset
	 * @return
	 */
	public static Properties loadFromProp(InputStream fileInputStream, String charset) {
		Properties properties = new Properties();
		try {
			properties.load(new InputStreamReader(fileInputStream, charset));
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return properties;
	}

	/**
	 * 读取property 或者 xml配置文件
	 * 
	 * @param file
	 * @param charset
	 * @return
	 */
	public static Properties fileToProperties(File file, String charset) {
		FileInputStream fileInputStream = null;
		Properties properties = null;
		try {
			String ext = FileUtils.getFilenameExtension(file.getName()).toLowerCase();
			fileInputStream = new FileInputStream(file);
			if (".xml".equals(ext))
				properties = loadFromXML(fileInputStream);
			else
				properties = loadFromProp(fileInputStream, charset);
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		} finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return properties;
	}

	/**
	 * 读取property 或者 xml配置文件
	 * 
	 * @param file
	 * @return
	 */
	public static Properties fileToProperties(File file) {
		return fileToProperties(file, "utf-8");
	}

	/**
	 * 读取property 或者 xml配置文件
	 * 
	 * @param filePath
	 * @param charset
	 * @return
	 */
	public static Properties fileToProperties(String filePath, String charset) {
		return fileToProperties(new File(filePath), charset);
	}

	/**
	 * 读取property 或者 xml配置文件
	 * 
	 * @param filePath
	 * @return
	 */
	public static Properties fileToProperties(String filePath) {
		return fileToProperties(new File(filePath), "utf-8");
	}

	/**
	 * mapToProperties
	 * 
	 * @see map convert object value to string value
	 * @param m
	 * @return
	 */
	public static Properties mapToProperties(Map<String, Object> m) {
		Properties properties = null;
		if ((m != null) && (!m.isEmpty())) {
			properties = new Properties();
			properties.putAll(m);
		}
		return properties;
	}
}
